package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.MachineStatusDao;
import com.tbit.main.pojo.*;
import com.tbit.main.service.MachineStatusService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("machineStatusService")
public class MachineStatusServiceImpl implements MachineStatusService {
    @Autowired
    private MachineStatusDao machineStatusDao;

    @Override
    public MachineStatus getByMachineId(Integer machineId) {
        return machineStatusDao.getByMachineId(machineId);
    }

    @Override
    public List<MachineStatus> getByMachineIdBatch(List<Integer> machineIds) {
        List<MachineStatus> machineStatuses = new LinkedList<>();
        List<List<Integer>> machineIdList = ListUtils.partition(machineIds, 900);
        for (List<Integer> machineIdI : machineIdList) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("machineIds", StringUtil.getListString(machineIdI));
            machineStatuses.addAll(machineStatusDao.getByMachineIdBatch(params));
        }
        return machineStatuses;
    }

    @Override
    public List<MachineStatus> getByAccountId(Integer accountId) {
        return machineStatusDao.getByAccountId(accountId);
    }

    @Override
    public List<MachineStatus> getByRegionIds(List<Integer> regionId) {
        return machineStatusDao.getByRegionIds(StringUtil.getListString(regionId));
    }

    @Override
    public List<MachineStatus> getSocByAccountId(Integer accountId, Integer socPercent) {
        Map<String, Object> params = new HashMap<>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("socPercent", socPercent);
        return machineStatusDao.getSocByAccountId(params);
    }

    @Override
    public List<MachineStatus> getSocByKey(Integer accountId, Integer startSoc, Integer endSoc) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("startSoc", startSoc);
        params.put("endSoc", endSoc);
        return machineStatusDao.getSocByKey(params);
    }

    @Override
    public List<MachineStatus> getSocByRegionIdAndKey(List<Integer> regionId, Integer startSoc,
                                                      Integer endSoc) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("startSoc", startSoc);
        params.put("endSoc", endSoc);
        return machineStatusDao.getSocByRegionIdAndKey(params);
    }

    @Override
    public List<MachineStatusBorrow> getUnUseByStartEnd(Integer accountId,
                                                        String start, String end) {
        Map<String, Object> params = new HashMap<>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("start", start);
        params.put("end", end);
        return machineStatusDao.getUnUseByStartEnd(params);
    }

    @Override
    public List<MachineStatusBorrow> getAccountIdleTimeByStartEnd(Integer accountId, Integer startIdleTime, Integer endIdleTime) {
        Map<String, Object> params = new HashMap<>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("startIdleTime", startIdleTime);
        params.put("endIdleTime", endIdleTime);
        return machineStatusDao.selectAccountIdleTimeByStartEnd(params);
    }

    @Override
    public List<MachineStatusBorrow> getRegionIdleTimeByStartEnd(List<Integer> regionId, Integer startIdleTime, Integer endIdleTime) {
        Map<String, Object> params = new HashMap<>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("start", startIdleTime);
        params.put("end", endIdleTime);
        return machineStatusDao.selectRegionIdIdleTimeByStartEnd(params);
    }

    @Override
    public List<MachineStatusBorrow> getRegionByStartEnd(List<Integer> regionId, String start, String end) {
        Map<String, Object> params = new HashMap<>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("start", start);
        params.put("end", end);
        return machineStatusDao.getRegionByStartEnd(params);
    }

    @Override
    public List<MachineStatus> getSocByAccountIds(List<Integer> accountIds, Integer socPercent) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        params.put("socPercent", socPercent);
        return machineStatusDao.getSocByAccountIds(params);
    }

    @Override
    public List<MachineStatus> getSocByRegionId(List<Integer> regionId, Integer socPercent) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("socPercent", socPercent);
        return machineStatusDao.getSocByRegionId(params);
    }

    @Override
    public Integer getSocByAccountIdCount(Integer accountId, Integer socPercent) {
        Map<String, Object> params = new HashMap<>(4);
        params.put(DaoParamConstant.accountId, accountId);
        params.put("socPercent", socPercent);

        return machineStatusDao.getSocByAccountIdCount(params);
    }

    @Override
    public List<MachineStatusBorrow> getUnUseByAccountId(Integer accountId, String lastBorrowTime) {
        Map<String, Object> params = new HashMap<>(4);
        params.put(DaoParamConstant.accountId, accountId);
        params.put("lastBorrowTime", lastBorrowTime);
        return machineStatusDao.getUnUseByAccountId(params);
    }

    @Override
    public List<MachineStatusBorrow> getUnUseByRegionId(List<Integer> regionId, String lastBorrowTime) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("lastBorrowTime", lastBorrowTime);
        return machineStatusDao.getUnUseByRegionId(params);
    }

    @Override
    public List<MachineStatusBorrow> getBorrowByMachineIds(List<Integer> machineIds, Integer state) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineIds", StringUtil.getListString(machineIds));
        params.put("state", state);
        return machineStatusDao.getBorrowByMachineIds(params);
    }

    @Override
    public List<MachineStatusBorrow> getStatusAll() {
        return machineStatusDao.getStatusAll();
    }

    @Override
    public List<MachineStatus> getFaultByAccountId(Integer accountId, List<Integer> from) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("from", from);
        return machineStatusDao.getFaultByAccountId(params);
    }

    @Override
    public List<MachineStatus> getFaultByRegionId(List<Integer> regionId, List<Integer> from) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("from", from);
        return machineStatusDao.getFaultByRegionId(params);
    }

    @Override
    public List<MachineStatusBorrow> getUnUseByAccountIdList(List<OrderJob> orderJobs) {
        return machineStatusDao.getUnUseByAccountIdList(orderJobs);
    }

    @Override
    public List<MachineStatus> getByListAccountId(List<Integer> listAccountId) {
        return machineStatusDao.getByListAccountId(listAccountId);
    }


    @Override
    public List<Machine> getByAccountIdAll(List<Integer> accountId) {
        return machineStatusDao.getByAccountIdAll(accountId);
    }

    @Override
    public List<Map<String, Object>> getGroupByAccountIdLockPower() {
        return machineStatusDao.getGroupByAccountIdLockPower();
    }

    @Override
    public List<Integer> getByPosDt(String posDt) {
        return machineStatusDao.getByPosDt(posDt);
    }

    @Override
    public List<MachineStatus> getAccountIdsPosDt(List<Integer> accountIds, String posDt) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("posDt", posDt);
        return machineStatusDao.getAccountIdsPosDt(map);
    }

    @Override
    public List<Machine> getByBatDt(Integer accountId, String batDt, Integer batteryEU) {
        Map<String, Object> map = new HashMap<>();
        map.put("batDt", batDt);
        map.put(DaoParamConstant.accountId, accountId);
        map.put("batteryEU", batteryEU);
        return machineStatusDao.getByBatDt(map);
    }

    @Override
    public Integer getByBatDtId(Integer machineId, String batDt) {
        Map<String, Object> map = new HashMap<>();
        map.put("batDt", batDt);
        map.put("machineId", machineId);
        return machineStatusDao.getByBatDtId(map);
    }

    @Override
    public List<Machine> getByAccountIdPosDt(String posDt, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("posDt", posDt);
        map.put(DaoParamConstant.accountId, accountId);
        return machineStatusDao.getByAccountIdPosDt(map);
    }

    @Override
    public List<MachineStatus> getMachineStatusAll() {
        return machineStatusDao.getMachineStatusAll();
    }

    @Override
    public List<MachineStatus> getStream(List<Integer> regionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("updateTime", DateTimeUtil.getDayAgoTime(1));
        return machineStatusDao.getStreams(map);
    }

    @Override
    public List<MachineStatus> getAccountIdNotRegionId(List<Integer> accountIds, String posDt) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("posDt", posDt);
        return machineStatusDao.getAccountIdNotRegionId(map);
    }

    @Override
    public List<Integer> getAccountIdBorrow(Integer accountId, Integer startSoc, Integer endSoc) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("startSoc", startSoc);
        map.put("endSoc", endSoc);
        return machineStatusDao.getAccountIdBorrow(map);
    }

    @Override
    public List<Integer> getExist(List<Integer> list, Integer soc) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("soc", soc);
        return machineStatusDao.getExist(map);
    }

    @Override
    public void updateStatus(Point point, List<Integer> machineId, Integer posType) {
        Map<String, Object> map = new HashMap<>();
        map.put("lon", point.getX());
        map.put("lat", point.getY());
        map.put("machineId", StringUtil.getListString(machineId));
        map.put("posType", posType);
        map.put("posDt", DateTimeUtil.getNowTime());
        machineStatusDao.updateStatus(map);
    }

    @Override
    public void updateStatusPosBatch(List<MachineStatusPos> machineStatusPosList) {
        machineStatusDao.updateStatusPosBatch(machineStatusPosList);
    }

    @Override
    public void insert(Integer machineId, Point point, Integer posType) {
        Map<String, Object> map = new HashMap<>();
        map.put("lon", point.getX());
        map.put("lat", point.getY());
        map.put("machineId", machineId);
        map.put("socPercent", 0);
        map.put("surplusMileage", 0);
        map.put("posDt", DateTimeUtil.getNowTime());
        map.put("batDt", DateTimeUtil.getNowTime());
        map.put("posType", posType);
        machineStatusDao.insert(map);
    }

    @Override
    public List<MachineStatus> getByTag(int accountId, List<Integer> regionId, List<Integer> tagIds) {
        Map<String, Object> params = new HashMap<>();
        if (CollUtil.isNotEmpty(regionId)) {
            params.put("regionId", StringUtil.getListString(regionId));
        }
        params.put("tagIds", StringUtil.getListString(tagIds));
        params.put("accountId", accountId);
        return machineStatusDao.getByTag(params);
    }

    @Override
    public List<MachineStatus> getLongTermNonPos(Integer accountId, List<Integer> regionId, Integer hours) {
        Map<String, Object> params = new HashMap<>();
        if (CollUtil.isNotEmpty(regionId)) {
            params.put("regionId", StringUtil.getListString(regionId));
        }
        params.put("hours", hours);
        params.put(DaoParamConstant.accountId, accountId);
        return machineStatusDao.selectLongTermNonPos(params);
    }

    @Override
    public List<MachineStatus> getLongOrder(Integer accountId, List<Integer> regionId, Integer hours) {
        Map<String, Object> params = new HashMap<>();
        if (CollUtil.isNotEmpty(regionId)) {
            params.put("regionId", StringUtil.getListString(regionId));
        }
        params.put("hours", hours);
        params.put(DaoParamConstant.accountId, accountId);
        return machineStatusDao.selectLongOrder(params);
    }

    @Override
    public List<MachineStatus> getNoOrder(Integer accountId, List<Integer> regionId, Integer hours) {
        return machineStatusDao.getNoOrder(accountId, regionId, hours);
    }

}